cc.Class({
    extends: cc.Component,

    properties: {

    },

    editor: {
        executionOrder: 1
    },

    start() {

    },

    onLoad() {
        this.player = cc.find('player').getComponent('player')
        this.draw()
        this.listen()
    },

    /*
    * 绘制手柄：轨迹球 & 按钮
    */
    draw() {
        const
            trackball = cc.find('trackball', this.node).addComponent(cc.Graphics),
            ball = cc.find('trackball/ball', this.node).addComponent(cc.Graphics),
            btnA = cc.find('a', this.node).addComponent(cc.Graphics)

        trackball.fillColor = new cc.Color(255, 255, 255, 128)
        trackball.strokeColor = new cc.Color(255, 255, 255, 128)
        trackball.lineWidth = 2
        trackball.circle(
            trackball.node.width / 2,
            trackball.node.height / 2,
            trackball.node.height / 2
        )
        trackball.fill()
        trackball.stroke()

        ball.fillColor = new cc.Color(3, 169, 244, 128)
        ball.strokeColor = new cc.Color(255, 255, 255, 128)
        ball.lineWidth = 2
        ball.circle(ball.node.width / 2, ball.node.height / 2, ball.node.height / 2)
        ball.fill()
        ball.stroke()

        btnA.fillColor = new cc.Color(76, 175, 80, 128)
        btnA.strokeColor = new cc.Color(255, 255, 255, 128)
        btnA.lineWidth = 2
        btnA.circle(btnA.node.width / 2, btnA.node.height / 2, btnA.node.height / 2)
        btnA.fill()
        btnA.stroke()
    },

    listen() {
        const
            ball = cc.find('trackball/ball', this.node),
            btnA = cc.find('a', this.node),
            player = this.player,
            tracer = {}

        let lastKey = null

        ball.on(
            cc.Node.EventType.TOUCH_START,
            function(ev) {
                const d = cc.pDistance(this.convertTouchToNodeSpaceAR(ev.touch), cc.v2(0, 0))

                if (d <= this.width / 2) {
                    tracer.down = true
                    tracer.ox = this.x
                    tracer.oy = this.y
                    tracer.dx = ev.getLocationX()
                    tracer.dy = ev.getLocationY()
                }
            }
        )

        ball.on(
            cc.Node.EventType.TOUCH_MOVE,
            function(ev) {
                if (tracer.down) {
                    this.x = tracer.ox + ev.getLocationX() - tracer.dx
                    this.y = tracer.oy + ev.getLocationY() - tracer.dy

                    if (this.x ** 2 + this.y ** 2 > 25 ** 2) {
                        if (this.x === 0) {
                            const sign = this.y < 0 ? -1 : 1
                            this.y = 25 * sign
                        } else {
                            const
                                k = this.y / this.x,
                                sign = this.x < 0 ? -1 : 1

                            this.x = 25 / Math.sqrt(k * k + 1) * sign
                            this.y = this.x * k
                        }
                    }

                    if (this.x < 0) player.node.emit('left')
                    if (this.x > 0) player.node.emit('right')

                }
            }
        )

        ball.on(
            cc.Node.EventType.TOUCH_END,
            function() {
                tracer.down = false
                this.runAction(cc.moveTo(.1, 0, 0))
                player.node.emit('stand')
            }
        )

        ball.on(
            cc.Node.EventType.TOUCH_CANCEL,
            function() {
                tracer.down = false
                this.runAction(cc.moveTo(.1, 0, 0))
                player.node.emit('stand')
            }
        )

        btnA.on(
            cc.Node.EventType.TOUCH_START,
            function(ev) {
                this.runAction(cc.scaleTo(.1, 1.2))
                player.node.emit('jump')
            }
        )

        btnA.on(
            cc.Node.EventType.TOUCH_END,
            function(ev) {
                this.runAction(cc.scaleTo(.1, 1))
                player.node.emit('fall')
            }
        )

        btnA.on(
            cc.Node.EventType.TOUCH_CANCEL,
            function(ev) {
                this.runAction(cc.scaleTo(.1, 1))
                player.node.emit('fall')
            }
        )

        cc.systemEvent.on(
            cc.SystemEvent.EventType.KEY_DOWN,
            function(ev) {
                let en = null

                if (ev.keyCode === cc.KEY.left) {
                    en = 'left'
                } else if (ev.keyCode === cc.KEY.right) {
                    en = 'right'
                } else if (ev.keyCode === cc.KEY.space) {
                    en = 'jump'
                }

                lastKey !== ev.keyCode && player.node.emit(en)
                lastKey = ev.keyCode
            }
        )

        cc.systemEvent.on(
            cc.SystemEvent.EventType.KEY_UP,

            function(ev) {
                if (lastKey === ev.keyCode) lastKey = null
                if (ev.keyCode === cc.KEY.left) player.node.emit('stand')
                if (ev.keyCode === cc.KEY.right) player.node.emit('stand')
                if (ev.keyCode === cc.KEY.space) player.node.emit('fall')
            }
        )
    }
})
